/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.repository.common;

import com.inspur.edp.cef.api.attr.I18nResourceAttribute;
import com.inspur.edp.cef.api.attr.PropertyInfosAttribute;
import com.inspur.edp.cef.gencommon.ReflectionUtils;
import com.inspur.edp.cef.generator.repository.RepositoryGenUtil;
import com.inspur.edp.cef.spi.entity.AbstractPropertyInfo;
import com.inspur.edp.cef.spi.entity.info.AbstractDataTypeInfo;

import java.lang.reflect.Type;

public class CefEntityTypeInfo {
	private Class _actionInterfaceType;

	private Class privateDataInterfaceType;

	public final Class getDataInterfaceType() {
		return privateDataInterfaceType;
	}

	public final void setDataInterfaceType(Class value) {
		privateDataInterfaceType = value;
	}

	public Class getActionInterfaceType() {
		return _actionInterfaceType;
	}

	public final void setActionInterfaceType(Class value) {
		propInfos = null;
		_actionInterfaceType = value;
	}

	private Class privateActionImplType;

	public final Class getActionImplType() {
		return privateActionImplType;
	}

	public final void setActionImplType(Class value) {
		privateActionImplType = value;
	}

	private Class privateAllInterfaceType;

	public final Class getAllInterfaceType() {
		return privateAllInterfaceType;
	}

	public final void setAllInterfaceType(Class value) {
		privateAllInterfaceType = value;
	}

	private Class privateGeneratedImplType;

	public final Class getGeneratedImplType() {
		return privateGeneratedImplType;
	}

	public final void setGeneratedImplType(Class value) {
		privateGeneratedImplType = value;
	}

	private Type entityInfoType;

	public final Type getEntityInfoType() {
		if (entityInfoType == null && _actionInterfaceType != null) {
			PropertyInfosAttribute propAttr = (PropertyInfosAttribute) ReflectionUtils.getAnnotation(_actionInterfaceType.getClass(), PropertyInfosAttribute.class);
			if (propAttr == null) {
				I18nResourceAttribute entityInfoAttr = (I18nResourceAttribute) ReflectionUtils.getAnnotation(_actionInterfaceType, I18nResourceAttribute.class);
				if (entityInfoAttr != null && entityInfoAttr.resourceInfoType() != null) {
					this.entityInfoType =entityInfoAttr.resourceInfoType();
				}
			}
		}
		return entityInfoType;
	}

	private java.util.ArrayList<AbstractPropertyInfo> propInfos;

	public final java.util.ArrayList<AbstractPropertyInfo> getPropInfos() {
		if (propInfos == null && _actionInterfaceType != null) {
			propInfos = new java.util.ArrayList<AbstractPropertyInfo>();
			PropertyInfosAttribute propAttr = (PropertyInfosAttribute) ReflectionUtils.getAnnotation(_actionInterfaceType.getClass(), PropertyInfosAttribute.class);
			if (propAttr == null) {
				I18nResourceAttribute entityInfoAttr = (I18nResourceAttribute) ReflectionUtils.getAnnotation(_actionInterfaceType, I18nResourceAttribute.class);
				if (entityInfoAttr != null && entityInfoAttr.resourceInfoType() != null) {
					AbstractDataTypeInfo entityInfo = (AbstractDataTypeInfo) RepositoryGenUtil.createInstanceByClass(entityInfoAttr.resourceInfoType());
					for (Class t : entityInfo.getProptyInfoTypes().values()) {
						propInfos.add((AbstractPropertyInfo) RepositoryGenUtil.createInstanceByClass(t));
					}
				}
			} else {
				if (propAttr.types() != null) {
					for (Class t : propAttr.types()) {
						propInfos.add((AbstractPropertyInfo) RepositoryGenUtil.createInstanceByClass(t));
					}
				}
			}
		}
		return propInfos;
	}
}
